package com.my.demo.auth;

import com.google.common.collect.Lists;
import com.my.demo.auth.entity.Account;
import com.my.demo.auth.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

import javax.annotation.Resource;

/**
 * Created by LeiXuMeng on 2017/7/14.
 */
@SpringBootApplication
@EnableAuthorizationServer
public class AuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

    @Resource
    private AuthenticationProvider authenticationProvider;
    @Resource
    private AccountRepository accountRepository;


    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Lists.newArrayList(authenticationProvider));
    }

    @Autowired
    public void init(){
        try {
            Account account=new Account();
            account.setName("zhangsan");
            account.setPassword("123456");
            account.setRoles(new String []{"ROLE_USER"});
            accountRepository.deleteAll();
            accountRepository.save(account);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
